package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.DTO.*;
import com.ruoyi.framework.web.service.PurchaseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.stream.Collectors;

//购买商品
@Slf4j
@RestController
@RequestMapping("/purchase/record")
public class PurchaseRecordController extends BaseController {
    @Autowired
    private PurchaseRecordService purchaseRecordService;

    /**
     * 支付宝APP支付接口
     * @param purchaseRecordBO 请求参数
     * @return 支付宝响应
     */
        @PostMapping("/app/payment")
    public AjaxResult appPayment(@RequestBody @Validated PurchaseRecordBO purchaseRecordBO) {
        return success(purchaseRecordService.appPayment(purchaseRecordBO));
    }

    /**
     * 支付宝网页支付接口
     * @param purchaseRecordBO 请求参数
     * @return 支付宝响应
     */
    @PostMapping("/wap/payment")
    public AjaxResult wapPayment(@RequestBody @Validated PurchaseRecordBO purchaseRecordBO) {
        return success(purchaseRecordService.wapPayment(purchaseRecordBO));
    }

    @PostMapping("/callback")
    @Anonymous
    public AjaxResult callback(HttpServletRequest request) {
        // 直接获取参数Map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 如果需要转换为Map<String, String>格式
        Map<String, String> paramsMap = parameterMap.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> String.join(",", entry.getValue())
                ));
        log.info("转换后参数Map={}", paramsMap);
        purchaseRecordService.callback(paramsMap);
        return success();
    }

    @PostMapping("/user/list")
    public AjaxResult userList(@RequestBody @Validated SelectUserPurchaseRecordBO selectUserPurchaseRecordBO) {
        return success(purchaseRecordService.selectUserPurchaseRecord(selectUserPurchaseRecordBO));
    }

    /**
     * 管理员查看所有订单
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody @Validated SelectPurchaseRecordBO selectPurchaseRecordBO) {
        return success(purchaseRecordService.selectPurchaseRecord(selectPurchaseRecordBO));
    }

    /**
     * 管理员更新订单快递单号
     */
    @PostMapping("/update/trackingNumber")
    public AjaxResult updateTrackingNumber(@RequestBody UpdateTrackingNumberBO updateTrackingNumberBO) {
        purchaseRecordService.updateTrackingNumber(updateTrackingNumberBO);
        return success();
    }

    /**
     * 用户确认收货
     */
    @PostMapping("/confirm/receipt")
    public AjaxResult confirmReceipt(@RequestBody @Validated ConfirmReceiptBO confirmReceiptBO) {
        purchaseRecordService.confirmReceipt(confirmReceiptBO);
        return success();
    }
}
